package com.wg.process.controller;

import com.google.common.collect.Maps;
import com.wg.process.cmd.FindNextUserTaskNodeCmd;
import com.wg.process.core.ResultBody;
import com.wg.process.service.TaskServiceExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.common.engine.impl.interceptor.CommandExecutor;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/task")
@Api
public class TaskController {

    @Autowired
    TaskService taskService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    RepositoryService repositoryService;

    private CommandExecutor commandExecutor;
    @Autowired
    SpringProcessEngineConfiguration springProcessEngineConfiguration;

    @Autowired
    TaskServiceExt taskServiceExt;

    @RequestMapping(value = "start",method = RequestMethod.GET)
    @ApiOperation(value = "",notes = "新增之后返回对象")
    public ResultBody startTask(){
        Authentication.setAuthenticatedUserId("wangang");
        Map<String,Object> variable = Maps.newHashMap();
        variable.put("skip",true);
        variable.put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("crc_ems1",variable);
        System.out.println(processInstance);
        return ResultBody.success(processInstance);
    }

    @RequestMapping(value = "submitTask/{taskid}",method = RequestMethod.GET)
    @ApiOperation(value = "完成任务",notes = "新增之后返回对象")
    public ResultBody submitTask(@PathVariable String taskid){
        taskService.complete(taskid);
        return ResultBody.success();
    }

    @RequestMapping(value = "nextStep/{processId}/{nodeId}",method = RequestMethod.GET)
    @ApiOperation(value = "获取下一环节处理人",notes = "新增之后返回对象")
    public ResultBody nextStep(@PathVariable String processId,@PathVariable String nodeId){
        System.out.println(processId);
        ExecutionEntity execution = (ExecutionEntity) runtimeService.createExecutionQuery().processInstanceId(processId).activityId(nodeId).singleResult();
        commandExecutor =springProcessEngineConfiguration.getCommandExecutor();
        return ResultBody.success(commandExecutor.execute(new FindNextUserTaskNodeCmd(repositoryService,execution,execution.getProcessDefinitionId())));
    }

    @ApiOperation(value = "获取正在执行的流程图",notes = "")
    @RequestMapping(value = "processDiagram/{processId}",method = RequestMethod.GET)
    public void genProcessDiagram(HttpServletResponse httpServletResponse,@PathVariable String processId) throws Exception {
        taskServiceExt.genProcessDiagram(httpServletResponse,processId);
    }
}
